package com.rongeadi.app.jinrirong.activity.user;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.rongeadi.app.R;
import com.rongeadi.app.common.base.BaseMvpActivity;
import com.rongeadi.app.common.widget.IncomingRanking;
import com.rongeadi.app.jinrirong.activity.user.presenter.MyInfoPresenter;
import com.rongeadi.app.jinrirong.activity.user.view.RankingView;
import com.rongeadi.app.jinrirong.config.ApiFactory;
import com.rongeadi.app.jinrirong.config.UserManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangsy on 2019/1/20.
 */

public class IncomeRankingActivity extends BaseMvpActivity<RankingView, MyInfoPresenter> implements RankingView {

    @BindView(R.id.x_listView_2)
    RecyclerView mRvHelpList;
    List list = new ArrayList<>();

    @Override
    protected int setContentLayoutRes() {
        return R.layout.income_ranking_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRvHelpList.setLayoutManager(new LinearLayoutManager(this));
        //todo 请求网络
        new Thread(() -> {
            mPresenter.requestIncomingRankingInfo(UserManager.getInstance().getToken());
        }).start();
    }

    @Override
    protected void initView() {
    }

    @Override
    protected MyInfoPresenter createPresenter() {
        return new MyInfoPresenter();
    }

    @Override
    public void showLoadingView() {
        super.showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }


    @Override
    public void onTokenInvalidate() {

    }

    @Override
    public void onNetworkConnectFailed() {

    }

    @Override
    public void onRemoteSucceed(JSONObject result) {
        mRvHelpList.setAdapter(new IncomeRankingViewAdapter(this, result));
    }

    @Override
    public void onRemoteFailed(String msg) {

    }

    class IncomeRankingViewAdapter extends RecyclerView.Adapter<IncomeRankingViewAdapter.ViewHolder> {

        private Context mContext;
        private LayoutInflater inflater;
        private JSONObject remoteResult;

        public IncomeRankingViewAdapter(Context mContext, JSONObject remoteResult) {
            this.mContext = mContext;
            this.inflater = LayoutInflater.from(mContext);
            this.remoteResult = remoteResult;
        }


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            ViewHolder holder = new ViewHolder(inflater.inflate(R.layout.ranking_item, viewGroup, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
            viewHolder.contentView.setLayoutManager(new LinearLayoutManager(mContext));
            JSONArray data = remoteResult.getJSONArray("data");
            List<IncomingRanking> list = new ArrayList();
            if (data != null && data.size() > 0) {
                for (Object obj : data) {
                    list.add(((JSONObject) obj).toJavaObject(IncomingRanking.class));
                }
            }
            //去掉前三个
            IncomingRanking num1 = list.remove(0);
            IncomingRanking num2 = list.remove(0);
            IncomingRanking num3 = list.remove(0);

            ((TextView) viewHolder.itemView.findViewById(R.id.rank_number1_mobile)).setText(num1.getMobile());
            ((TextView) viewHolder.itemView.findViewById(R.id.rank_number1_money)).setText(num1.getTotalAmount());

            ((TextView) viewHolder.itemView.findViewById(R.id.rank_number2_mobile)).setText(num2.getMobile());
            ((TextView) viewHolder.itemView.findViewById(R.id.rank_number2_money)).setText(num2.getTotalAmount());

            ((TextView) viewHolder.itemView.findViewById(R.id.rank_number3_mobile)).setText(num3.getMobile());
            ((TextView) viewHolder.itemView.findViewById(R.id.rank_number3_money)).setText(num3.getTotalAmount());


            viewHolder.contentView.setAdapter(new IncomeRankingViewItemAdapter(mContext, list));
            int index = remoteResult.getIntValue("me_index");
            StringBuffer str = new StringBuffer("本月佣金：");
            double doubleValue = remoteResult.getJSONObject("me_money").getDoubleValue("totalAmount");
            String x = "(";
            if (index > 10) {
                x = "（" + index + "名外）";
            } else {
                x = "（" + index + "名）";
            }
            str = str.append(doubleValue).append("元 ").append(x);
            ((TextView) viewHolder.itemView.findViewById(R.id.tv_myTotalAmount)).setText(str.toString());
            int imgId = 0;

            index = index > 1 ? index : 1;

            switch (index) {
                case 1:
                    imgId = R.drawable.ljt_icon_1;
                    break;
                case 2:
                    imgId = R.drawable.ljt_icon_2;
                    break;
                case 3:
                    imgId = R.drawable.ljt_icon_3;
                    break;
                default:
                    imgId = R.drawable.ic_user;
            }
            ((ImageView) viewHolder.itemView.findViewById(R.id.imv_myHead)).setImageResource(imgId);
        }

        @Override
        public int getItemCount() {
            return 1;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private RecyclerView contentView;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                contentView = itemView.findViewById(R.id.contentView);
            }
        }
    }

    class IncomeRankingViewItemAdapter extends RecyclerView.Adapter<IncomeRankingViewItemAdapter.ViewHolder> {

        private Context mContext;
        private List<IncomingRanking> list;
        private LayoutInflater inflater;
        MyInfoPresenter presenter;

        public IncomeRankingViewItemAdapter(Context mContext, List<IncomingRanking> list) {
            this.mContext = mContext;
            this.inflater = LayoutInflater.from(mContext);
            this.list = list;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new ViewHolder(inflater.inflate(R.layout.two_item, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
            ((TextView) holder.itemView.findViewById(R.id.tv_ranking)).setText(String.valueOf((i + 4)));
            ((TextView) holder.itemView.findViewById(R.id.account_mobile)).setText(list.get(i).getMobile());
            ((TextView) holder.itemView.findViewById(R.id.total_money)).setText("￥" + list.get(i).getTotalAmount());
            String img = String.valueOf(list.get(i).getHeadImg());

            if (!("".equals(img) || "null".equals(img))) {
                Glide.with(holder.itemView.getContext()).load(ApiFactory.RESOURCE_URL + img).into(((ImageView) holder.itemView.findViewById(R.id.headerImage)));
            } else {
                ((ImageView) holder.itemView.findViewById(R.id.headerImage)).setImageResource(R.mipmap.login_logo);
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }
}